Skip to content

Added queens problem solutions#4

Open
DolzhenkoAlexa wants to merge 1 commit intomainfrom
HW/Queens/Dolzhenko
Open

Added queens problem solutions#4
DolzhenkoAlexa wants to merge 1 commit intomainfrom
HW/Queens/Dolzhenko

Conversation

@DolzhenkoAlexa
Copy link
Owner

Добавлено 3 решения задачи с ферзями (переборное решение, рекурсивное и самое быстрое), объяснение их сложностей в файле complexity.md

Выполнено: Долженко Александра Борисовна

Copy link

@Godrik0 Godrik0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Решения почти правильные, есть что поправить

- Очень медленное решение
- all_cells содержит n^2 элементов (все клетки доски), и при помощи combinations получаем C(n^2, n)
- is_valid в худшем случае даст n^2 операций
Итог: О(С(n^2,n) * n^2)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно писать формулки как в latex, если обернуть их в $$
$O(C^{n^2}_n \cdot n^2)$

all_cells = [(i, j) for i in range(n) for j in range(n)]

# Возьмем все расстановки без повторяющихся клеток
for cords in itertools.combinations(all_cells, n):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вместо combinations по всем клеткам используй permutations для столбцов. Так перебор сократиться с 4.4 млрд до 40 тысяч вариантов (для N=8).

Comment on lines +25 to +27
if (row, column) not in placed_queens: # Проверка, чтобы не было двух (0,1)
new = placed_queens + [(row, column)]
count += queens_counter(row + 1, n, new)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно было добавить тут проверку is_valid, тогда, если перестановка уже в текущем виде не валидна, мы её отбросим. Сейчас сначала заполняется поле, а после выполняется проверка валидности, что не имеет особого смысла.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants